<?php
class languages extends MY_Model{
    public function __construct() {
        parent::__construct();
    }
    
    //insert language
    public function add_language($fields=array()){   
        if(empty($fields)){
            return false;
        }
        if($fields['is_default']==1){
            if($fields['actived']==0)
            {
                _set_error_message('This language is not actived so you cant make it default...');
                redirect('administrator/setting/language');
            }
            $this->set_default_language();
        }
        $this->db->insert('languages',$fields);
        return $this->db->insert_id();
    }
    
    //get language
    public function get_language($lang_id=false,$active=false){
        if($lang_id){
            $this->db->where('lang_id',$lang_id);
        }
        if($active)
                $this->db->where('actived',$active);
        $query = $this->db->get('languages');
        if($lang_id){
            return $query->row();
        }
        return $query->result();
    }
        
    private function set_default_language(){
        $this->db->where('is_default',1);
        $this->db->update('languages',array('is_default'=>0));
    }
    //get language default
    public function get_language_default($default=false,$active=false,$get_language_is_not_default = false){
        if($default){
            $this->db->where('is_default',$default);
            
        }elseif($get_language_is_not_default)
        {
            $this->db->where_not_in("is_default",1);
        }
        if($active){
            $this->db->where('actived',$active);
        }
        
        $query = $this->db->get('languages');
        if($default)
            return $query->row();
        return $query->result();
    }
    
    //update language
    public function update_language($fields=array(),$lang_id=false)
    {
        if($lang_id){
            if($fields['is_default']==1){
            if($fields['actived']==0)
            {
                _set_error_message('This language is not actived so you cant make it default...');
                redirect('administrator/setting/language');
            }
            $this->set_default_language();
        }
            $this->db->where('lang_id',$lang_id);
            $this->db->update('languages',$fields);
            return $this->db->affected_rows();
        }
        return false;
    }
    
    //delete language
    public function delete_language($lang_id=false){
        if($lang_id){
            if($this->get_language($lang_id)->is_default){
                _set_error_message('You cant delete default language');
                redirect('administrator/setting/language');
            }
            $this->db->where('lang_id',$lang_id);
            $this->db->delete('languages');
            return $this->db->affected_rows();
        }
        return false;    
    }
    
    public function get_languages($conditions=array()){
        if(empty($conditions))
            $query = $this->db->get('languages');
        else
            $query = $this->db->get_where('languages',$conditions);
       
        if(isset($conditions['lang_code']))
            return $query->row();
        return $query->result();
    }
}
?>